/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
 *
 * @brief load data instruction ut for ascend910B1
 *
 */
#include <gtest/gtest.h>
#include "kernel_operator.h"
#include "lib/matmul/tiling.h"
#include "impl/matmul/modules/matmul_param.h"
#include "impl/matmul/modules/matmul_policy.h"
#define private public
#include "impl/matmul/modules/stage/copy_cube_in/copy_cube_in_params.h"
#include "impl/matmul/modules/matmul_private_modules.h"
#include "base_tiling_struct.h"

using namespace std;
using namespace AscendC;
using namespace matmul;

namespace {
template <class A_TYPE, class B_TYPE, class C_TYPE, class BIAS_TYPE, const MatmulConfig& MM_CFG, class MM_CB,
MATMUL_POLICY_DEFAULT_OF(MatmulPolicy)>
class MatmulImpl: MATMUL_IMPORT_MODULE_PRIVATE(CopyCubeInParamsB)
, MATMUL_IMPORT_MODULE_PRIVATE(MatmulVarB)
, MATMUL_IMPORT_MODULE_PRIVATE(MatmulShapeInfoA)
, MATMUL_IMPORT_MODULE_PRIVATE(MatmulShapeInfoB)
{
    MATMUL_ALLOW_USING_PRIVATE(CopyCubeInParamsB);
    MATMUL_ALLOW_USING_PRIVATE(MatmulVarB);
    MATMUL_ALLOW_USING_PRIVATE(MatmulShapeInfoA);
    MATMUL_ALLOW_USING_PRIVATE(MatmulShapeInfoB);

public:
    using VAR_PARAMS =
        typename MatmulParams<A_TYPE, B_TYPE, C_TYPE, BIAS_TYPE, MM_CFG, GetMatmulVersion(MM_CFG)>::PARAMS;
    template<InputTypeTag TAG>
    using MatmulVar = typename AscendC::Conditional<TAG == InputTypeTag::A, MatmulVarB, MatmulVarB>::type;
    template <InputTypeTag TAG>
    using MatmulShapeInfo =
        typename AscendC::Conditional<TAG == InputTypeTag::A, MatmulShapeInfoA, MatmulShapeInfoB>::type;

    MatmulImpl() {}

    VAR_PARAMS& GetVar() {
        return var;
    }

    void InitVar(const TCubeTiling &tiling) {
        var.tiling_.SetTiling(&tiling);
        var.tpipe_ = &pipe;
    }

    void SetRuntimeParams(int32_t baseUseK, int32_t baseUseN, int32_t stepKbIdx = 0, int32_t stepNIdx = 0, bool isTranspose = false) {
        var.baseUseN_ = baseUseN;
        var.baseUseK_ = baseUseK;
        var.nIter_ = var.tiling_.GetSingleCoreN() / var.tiling_.GetBaseN();
        var.kIter_ = Ceil(var.tiling_.GetSingleCoreK(), var.tiling_.GetBaseK());
        var.singleCoreM_ = var.tiling_.GetSingleCoreM();
        var.singleCoreN_ = var.tiling_.GetSingleCoreN();
        var.singleCoreK_ = var.tiling_.GetSingleCoreK();
        N_ = var.tiling_.GetSingleCoreM();
        Kb_ = var.tiling_.GetSingleCoreK();
        var.isTransposeB_ = isTranspose;
        var.isB1KFullLoad_ = (var.tiling_.GetStepKb() >= var.kIter_);
        var.stepKbIdx_ = stepKbIdx;
        var.stepNIdx_ = stepNIdx;
        if constexpr (DoMatmulMDL(MM_CFG) || DoMatmulSpecialMDL(MM_CFG)) {
            var.nStepIter_ = Ceil(var.singleCoreN_, var.tiling_.GetBaseN() * var.tiling_.GetStepN());
            var.tailStepN_ = var.singleCoreN_ % (var.tiling_.GetBaseN() * var.tiling_.GetStepN());
            if (var.tailStepN_ == 0) {
                var.tailStepN_ = var.tiling_.GetBaseN() * var.tiling_.GetStepN();
            }
            var.kbStepIter_ = Ceil(var.singleCoreK_, var.tiling_.GetBaseK() * var.tiling_.GetStepKb());
            var.tailStepKb_ = var.singleCoreK_ % (var.tiling_.GetBaseK() * var.tiling_.GetStepKb());
            if (var.tailStepKb_ == 0) {
                var.tailStepKb_ = var.tiling_.GetBaseK() * var.tiling_.GetStepKb();
            }
        }
    }

private:
    TPipe pipe;
    VAR_PARAMS var;
    int32_t M_;
    int32_t N_;
    int32_t Ka_;
    int32_t Kb_;
};
}

class TestCopyCubeInParams : public testing::Test {
protected:
    void SetUp() {}
    void TearDown() {}

private:
    using A_TYPE = matmul::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, int8_t, false>;
    using B_TYPE = matmul::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, int8_t, false>;
    using C_TYPE = matmul::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, float>;
    using BIAS_TYPE = matmul::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, float>;

    MatmulImpl<A_TYPE, B_TYPE, C_TYPE, BIAS_TYPE, CFG_MDL, void> mm;
};

TEST_F(TestCopyCubeInParams, all_interface) {
    TilingParams tilingParams = {1, 64, 48, 256, 64, 48, 256, 32, 48, 96, 2, 4, 1, 2, 1, 3, 1, 1};
    TCubeTiling tiling;
    tilingParams.GetTiling(tiling);
    int32_t baseUseK = 96;
    int32_t baseUseN = 48;
    int32_t stepKbIdx = 1;
    int32_t stepNIdx = 1;
    mm.InitVar(tiling);
    mm.SetRuntimeParams(baseUseK, baseUseN, stepKbIdx, stepNIdx);
    EXPECT_EQ(mm.GetStepCol(), 2);
    EXPECT_EQ(mm.GetStepRow(), 3);
    EXPECT_EQ(mm.GetBufferPos(), stepKbIdx);
    EXPECT_EQ(mm.GetOrgHeightAlign(), 256);
    EXPECT_EQ(mm.GetOrgWidthAlign(), 64);
    EXPECT_EQ(mm.GetBaseHeightAlign(), 96);
    EXPECT_EQ(mm.GetBaseWidthAlign(), 64);
    EXPECT_EQ(mm.GetSingleHeightAlign(), 256);
    EXPECT_EQ(mm.GetSingleWidthAlign(), 64);
    EXPECT_EQ(mm.GetSingleSize(), 48*256);
    EXPECT_EQ(mm.GetSingleSizeAlign(), 64*256);
    EXPECT_EQ(mm.GetBufferSize(), 96 * 64);
}
